package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "category",allEntries = true )
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.addDish(dishDTO);
        log.info("新增菜品成功：{}",dishDTO.getName());
        return Result.success("新增成功");
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "category",allEntries = true )
    public Result<String> updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishService.updateDish(dishDTO);
        log.info("修改菜品成功：{}",dishDTO.getName());
        return Result.success("修改成功");
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "category",allEntries = true )
    public Result<String> deleteDishs(@RequestParam List<Long> ids){
        log.info("删除菜品：{}",ids);
        dishService.deleteDishs(ids);
        log.info("删除菜品成功：{}",ids);
        return Result.success("删除菜品成功");
    }

    @PostMapping("/status/{status}")
    @ApiOperation("停售商品")
    @CacheEvict(cacheNames = "category",allEntries = true )
    public Result<String> updateStatuDish(@PathVariable Integer status, Long id){
        log.info("停售商品：{}",id);
        dishService.updateStatuDish(id,status);
        return Result.success("已停售");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.getByID(id);
        log.info("数据回显：{}",dishVO);
        return Result.success(dishVO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishDTO){
        log.info("分页查询菜品：{}",dishDTO);
        PageResult pageResult = dishService.pageQuery(dishDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    @ApiOperation("查询菜品列表")
    public Result<List<DishVO>> pageQuery(Long categoryId,String name){
        log.info("查询菜品列表：{}",categoryId);
        List<DishVO> dishVO = dishService.dishList(categoryId,name);
        return Result.success(dishVO);
    }
}